
class ListNode(object):

    def __init__(self, val, next=None):
        self.val = val
        self.next = next


def len_linked_list(node):
    cnt = 0
    while node:
        cnt += 1
        node = node.next

    return cnt

def nth_to_last(head, k):
    # 1 --- 2 --- 3 --- 4 --- N, k = 2  (return 3)
    fast = head

    while fast and k:
        k -= 1
        fast = fast.next

    if k != 0:
        return None

    slow = head

    while fast:
        slow = slow.next
        fast = fast.next

    return slow

def build_test():
    v5 = ListNode(10)
    v4 = ListNode(8, v5)
    v3 = ListNode(6, v4)
    v2 = ListNode(4, v3)
    v1 = ListNode(2, v2)
    vm = ListNode(1, v1)

    return vm
